package com.slanmao.common.utils;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.slanmao.common.entity.MapResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * <CODE>LocalUtils</CODE>
 * 地理位置API
 * @author fenrir
 * @Date 19-3-7
 */

@Component
@Slf4j
public class LocationUtils {

    @Autowired
    private HttpUtils httpUtils;

    private static final String url = "https://restapi.amap.com/v3/geocode/regeo?" +
            "key=%s" +
            "&location=%s";

    private static final String key = "f54beda737decf1b10a7d19d7d6d8922";

    /**
     * 通过经纬度获取高德地理位置
     * @param lo 纬度
     * @param la 精度
     * @return
     */
    public String convert(String lo,String la){
        String localtion = lo+","+la;
        String format = String.format(url, key, localtion);
        String response = httpUtils.get(format);
        MapResponse mapResponse = new Gson().fromJson(response, MapResponse.class);
        if (mapResponse.getStatus().equals("1")){
            return mapResponse.getFullAddress();
        }else {
            log.info("请求高德错误:"+response);
            return null;
        }
    }
}
